package sunwul.hospital.cmn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sunwul.hospital.cmn.service.DictService;
import sunwul.hospital.common.result.Result;
import sunwul.hospital.common.result.ResultCodeEnum;
import sunwul.hospital.model.model.cmn.Dict;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author sunwul
 * @date 2023/11/1 0:04:26
 * @description
 */
@Api(tags = "数据字典管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "根据数据上级id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    @ApiOperation(value = "导出数据字典")
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse response){
        dictService.exportDictData(response);
    }

    @ApiOperation(value = "导入数据字典")
    @PostMapping("importData")
    public Result importDict(@RequestBody MultipartFile file){
        dictService.importDictData(file);
        return Result.ok();
    }

    @ApiOperation(value = "根据dict_code和value查询数据字典名称")
    @GetMapping("/getName/{dictCode}/{value}")
    public Result getName(@PathVariable String dictCode, @PathVariable String value){
        if ((StringUtils.isEmpty(dictCode) || dictCode.equals("null")) || StringUtils.isEmpty(value) || value.equals("null")){
            return Result.build(ResultCodeEnum.PARAM_ERROR.getCode(),ResultCodeEnum.PARAM_ERROR.getMessage());
        }
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_code", dictCode);
        wrapper.eq( "value", value);
        Dict dict = dictService.getOne(wrapper);
        /**
         * 在教学视频中, 只有顶级的字典dict_code才不为空, 此处改造了数据库中数据,
         * 使得所有字典都有dict_code(数据字典编码/类型),
         * 不再需要根据parent_id与value进行查询name
          */
        return Result.ok(dict.getName());
    }

    @ApiOperation(value = "根据dictCode获取同分类下的所有节点列表(含根节点)")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable String dictCode) {
        /**
         * 由于并没有按照教学视频的结构创建字典表, 此处直接通过dictCode与value=0查询库中数据
         * dictCode相同的为同类型数据字典, 同类型数据字典中value=0的为根类型(类型分组的描述)
         * 比如根类型为id=1000 parent_id=1 name=民族 value=0 dict_code=National
         * 那么这个分组类型下的类型为 id=1001 parent_id=1000 name=汉族 value=1 dict_code=National
         *                      id=1002 parent_id=1000 name=满族 value=2 dict_code=National
         *                      id=1003 parent_id=1000 name=壮族 value=3 dict_code=National .....
         *
         */
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }
}
